/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.medbeans.modules.pdf;

import java.io.File;
import java.util.ResourceBundle;
import javax.swing.JFileChooser;

import org.openide.util.NbBundle;

/** Simple panel for a dialog asking users to choose a PDF viewer.
 * Offers a file chooser.
 * @author Jesse Glick
 */
public class ReconfigureReaderPanel extends javax.swing.JPanel {

    private String exceptionType, exceptionMessage;
    private PDFSettings settings;
    private ResourceBundle bundle = NbBundle.getBundle (ReconfigureReaderPanel.class);
    
    public ReconfigureReaderPanel (String exceptionType, String exceptionMessage) {
        this.exceptionType = exceptionType;
        this.exceptionMessage = exceptionMessage;
        settings = PDFSettings.getDefault ();
        initComponents ();
        initAccessibility ();            
    }

    private void initAccessibility() {
        this.getAccessibleContext().setAccessibleDescription(bundle.getString ("LBL_reconfigure"));
        viewerField.getAccessibleContext().setAccessibleDescription(bundle.getString ("ACS_Field"));
        chooseButton.getAccessibleContext().setAccessibleDescription(bundle.getString ("ACS_LBL_choose"));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        topLabel = new javax.swing.JLabel();
        currentViewerLabel = new javax.swing.JLabel();
        viewerField = new javax.swing.JTextField();
        chooseButton = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        topLabel.setText(bundle.getString ("LBL_reconfigure"));
        topLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 12, 11);
        add(topLabel, gridBagConstraints);

        currentViewerLabel.setText(bundle.getString ("LBL_current_viewer"));
        currentViewerLabel.setLabelFor(viewerField);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 12, 12, 12);
        add(currentViewerLabel, gridBagConstraints);

        viewerField.setEditable(false);
        viewerField.setText(settings.getPDFViewer ().getPath ());
        viewerField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                viewerFieldFocusGained(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
        add(viewerField, gridBagConstraints);

        chooseButton.setMnemonic((bundle.getString ("LBL_choose_mnem")).charAt(0));
        chooseButton.setText(bundle.getString ("LBL_choose"));
        chooseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 12, 11);
        add(chooseButton, gridBagConstraints);

    }//GEN-END:initComponents

    // accessibility
    private void viewerFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_viewerFieldFocusGained
        viewerField.selectAll();
    }//GEN-LAST:event_viewerFieldFocusGained

    private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseButtonActionPerformed
        File dir = settings.getPDFViewer ().getParentFile ();
        JFileChooser chooser = ((dir != null) ? new JFileChooser (dir) : new JFileChooser ());
        if (chooser.showDialog (this, bundle.getString ("LBL_select_viewer")) ==
                JFileChooser.APPROVE_OPTION) {
            File nue = chooser.getSelectedFile ();
            settings.setPDFViewer (nue);
            viewerField.setText (nue.getPath ());
        }
    }//GEN-LAST:event_chooseButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel currentViewerLabel;
    private javax.swing.JTextField viewerField;
    private javax.swing.JLabel topLabel;
    private javax.swing.JButton chooseButton;
    // End of variables declaration//GEN-END:variables

}
